/* eslint-disable no-fallthrough */
/* eslint-disable default-case */
import axios from 'axios'
let http = axios.create({
    timeout: 10000,
    // baseURL: '/api'
})

http.interceptors.request.use(req => {
    let token = sessionStorage.getItem('token')
    token && (req.headers['Authorization'] = `Bearer ${token}`)
    return req
}, error => {
    console.log(error);
})

http.interceptors.response.use(resp => {
    resp.data.token && sessionStorage.setItem('token', resp.data.token)
    return resp
}, error => {
    if (error && error.response) {
        let _resp = error.response
        console.log(_resp.status);
        switch (_resp.status) {
            case 404:
                alert('找不到页面')
            case 401:
                sessionStorage.removeItem('token')
                sessionStorage.removeItem('user')
                alert('身份已经过期,请重新登录')
            case 500:
                alert('服务器报错了')
        }
        return Promise.reject(error.response.data)
    } else {
        // alert('连接超时')
        return Promise.reject(error)
    }
})
export default http